<template>
        <view>
            <button @tap="startRecord">开始录音</button>
			<button @tap="pause">pause()  </button>
			<button @tap="resume">resume()  </button>
            <button @tap="endRecord">停止录音</button>
			
            <button @tap="playVoice">播放录音</button>
        </view>
    </template>
<script> 
const recorderManager = uni.getRecorderManager();
const music = uni.createInnerAudioContext(); 
    music.autoplay = true; 
    export default {
        data(){
           return{
			   text: 'uni-app',
			   voicePath: ''
		   }
        },
        onLoad() {
            let self = this;
            recorderManager.onStop(function (res) {
                console.log('recorder stop' + JSON.stringify(res));
                self.voicePath = res.tempFilePath;
            });
        },
        methods: {
            startRecord() {
                console.log('开始录音'); 
                recorderManager.start();
            },
			pause() {
				recorderManager.pause()
			},
			resume() {
				recorderManager.resume()
			},
			
			endRecord() {
			    console.log('录音结束');
			    recorderManager.stop(); //  music.destroy()
			},
           
            playVoice() {
                console.log('播放录音'); 
                if (this.voicePath) {
                    music.src = this.voicePath;
                    music.play();
                }
            },
			uploadRecord(){
				api.uploadFile({
					url: '/portal/record/record_upload', //上传接口
					header: {'content-type': 'application/x-www-form-urlencoded'},
					filePath: music.src,
					name: 'file',
					formData: {'user_nick': this.id,'src':    music.src},
					success: (uploadFileRes) => {
						uni.showToast({ title: '朗读成功', duration: 2000 });
						this.hasup = true;  //避免重复提交
					}
				});
			},
        }
    }
</script>
